/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package opensocial.apps.friendforsale.command;

import java.util.Date;

import opensocial.apps.common.Message;
import opensocial.apps.friendforsale.model.Activity;
import opensocial.apps.friendforsale.model.ActivityType;
import opensocial.apps.friendforsale.model.Player;

import org.apache.commons.chain.Context;
import org.apache.log4j.Logger;

/**
 * abstract class of dostuff accept request targetid=xxx
 * @author Sirius
 * 
 */
public abstract class DostuffCommand extends FriendsaleBaseCommand {
  private static final Logger LOGGER = Logger.getLogger(DostuffCommand.class);

  protected Player viewer;
  protected Player target;
  protected String messageId;

  /**
   * pre check the messageId is not null
   * and viewer target not null
   */
  @Override
  public boolean pre(Context context) {
    if (super.pre(context) == NEXT) {
      if (checkMessage() == END) {
        output(new Message(MessageId.PARAM_ERROR, null));
        return END;
      }

      if (loadPlayer() == END) {
        output(new Message(MessageId.PARAM_ERROR, null));
        return END;
      }

      return NEXT;
    } else {
      return END;
    }
  }

  @Override
  public abstract boolean execute() throws Exception;

  public boolean checkMessage() {
    if (getMessageId() == null) {
      LOGGER.error("initial dostuff command error:messageId is null");
      return END;
    } else {
      return NEXT;
    }
  }

  public boolean loadPlayer() {
    String targetId = request.getParameter("targetid");

    if (targetId == null) {
      LOGGER.error("target id is null");
      return END;
    }

    viewer = this.getViewer();
    target = this.getPlayer(targetId);

    if (viewer == null) {
      LOGGER.info("param error: viewer is null,viewerid:" + vid + " container:"
          + ctn);
      return END;
    }
    if (target == null) {
      LOGGER.info("param error: target is null,targetid:" + targetId
          + " container:" + ctn);
      return END;
    }

    return NEXT;
  }

  /**
  * use viewer and target create a activity object
  * @param type activity type
  * @return new Activity created
  */
  public Activity newActivity(ActivityType type) {
    Activity activity = new Activity();
    activity.setContainer(viewer.getContainer());
    activity.setActor(viewer.getUserId());
    activity.setActorName(viewer.getName());
    activity.setTarget(target.getUserId());
    activity.setTargetName(target.getName());
    activity.setTime(new Date().getTime());
    activity.setType(type);
    activity.setCommand(getMessageId());

    return activity;
  }
  
  public String getMessageId() {
    return messageId;
  }

  public void setMessageId(String messageId) {
    this.messageId = messageId;
  }


}
